import axios from 'axios';

const BASE_URL = 'http://127.0.0.1:8000/api/comment';

// 获取博客的所有评论
export const getCommentsByBlogId = async (blogId) => {
  try {
    const response = await axios.get(`${BASE_URL}/blog/${blogId}`);
    return response.data;
  } catch (error) {
    throw new Error(error.response.data.message || 'Failed to fetch comments');
  }
};

// 添加评论
export const addComment = async (blog_id, content, token) => {
  try {
    const response = await axios.post(`${BASE_URL}/add`, { blog_id, content }, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });
    return response.data;
  } catch (error) {
    throw new Error(error.response.data.message || 'Failed to add comment');
  }
};

// 删除评论
export const deleteComment = async (comment_id, token) => {
  try {
    const response = await axios.delete(`${BASE_URL}/${comment_id}`, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });
    return response.data;
  } catch (error) {
    throw new Error(error.response.data.message || 'Failed to delete comment');
  }
};

// 回复评论
export const replyComment = async (comment_id, content, token) => {
  try {
    const response = await axios.post(`${BASE_URL}/reply`, { comment_id, content }, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });
    return response.data;
  } catch (error) {
    throw new Error(error.response.data.message || 'Failed to reply to comment');
  }
};
